package com.tyd.cams;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.tyd.cams.Adapter.JxlRecyclerAdapter;
import com.tyd.cams.entity.JsInfoBean;
import com.tyd.cams.entity.JxLouBeans;
import com.tyd.cams.utils.SPUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

import static com.xuexiang.xui.XUI.getContext;

public class XuexiaojiaoshiActivity extends AppCompatActivity {
    RecyclerView JxlRecyclerView, JslRecyclerView;
    JxlRecyclerAdapter Jxladapter;
    JsRecyclerAdapter jsRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexiaojiaoshi);
        getJsList();
    }

    private void getJsList() {
        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/building/mgetjsinfo")
                .addHeader("Authorization", SPUtils.getToken())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(XuexiaojiaoshiActivity.this, "教室查询链接服务器超时", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("获取教学楼返回信息", response);
                        Gson son = new Gson();

//                        List<Map<String, List<JsInfoBean>>> list = son.fromJson(response, List.class);
                       List<JxLouBeans> jxLouBeans = son.fromJson(response, new TypeToken<List<JxLouBeans>>() {}.getType());
                        JxlRecyclerView = findViewById(R.id.jxl_info);
                        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                        Jxladapter = new JxlRecyclerAdapter(XuexiaojiaoshiActivity.this, jxLouBeans);
                        JxlRecyclerView.setLayoutManager(new LinearLayoutManager(XuexiaojiaoshiActivity.this, LinearLayoutManager.VERTICAL, false));
                        JxlRecyclerView.setAdapter(Jxladapter);
                     /*  for (JsInfoBean map : json) {
                            System.out.println("map"+map);

                            for (String s : map.keySet()) {
                                List<JsInfoBean> jsList = new ArrayList<JsInfoBean>();
                                System.out.println("s"+s);
                                for (JsInfoBean jsInfoBean : map.get(s)) {

                                    System.out.println("教室："+jsInfoBean.toString());
                                    jsList.add(jsInfoBean);
                                }
                                  Log.i("教学楼信息", jsList.toString());*/
//                                JslRecyclerView = findViewById(R.id.js_Recycle);

//                                Toast.makeText(XuexiaojiaoshiActivity.this, "操作成功" + jsList, Toast.LENGTH_SHORT).show();
//                                jsRecyclerAdapter = new JsRecyclerAdapter(XuexiaojiaoshiActivity.this, (List<JsInfoBean>) jsList);
//                                JxlRecyclerView.setLayoutManager(new LinearLayoutManager(XuexiaojiaoshiActivity.this, LinearLayoutManager.VERTICAL, false));

//                                JslRecyclerView.setLayoutManager(new LinearLayoutManager(XuexiaojiaoshiActivity.this, LinearLayoutManager.VERTICAL, false));
//                                JslRecyclerView.setAdapter(jsRecyclerAdapter);
                         /*   }
                        }*/

                        System.out.println(jxLouBeans.toString());
                        /*for (LinkedTreeMap<String, List<JiaoXloubean>> o : list) {
                            System.out.println(o);
                        }*/

                        Log.i("教学楼信息", jxLouBeans.toString());
                        //设置适配器
                       /* JxlRecyclerView = findViewById(R.id.jxl_info);
                        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                        Jxladapter = new JxlRecyclerAdapter(XuexiaojiaoshiActivity.this, jxLouBeans);
                        JxlRecyclerView.setLayoutManager(new LinearLayoutManager(XuexiaojiaoshiActivity.this, LinearLayoutManager.VERTICAL, false));
                        JxlRecyclerView.setAdapter(Jxladapter);*/


                    }
                })).start();
    }

    private void setjsInfo(int bid) {
        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/building/mgetjs")
                .addHeader("Authorization", SPUtils.getToken())
                .addParams("Bid", String.valueOf(bid))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(XuexiaojiaoshiActivity.this, "教室获取失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson son = new Gson();
                        List<JsInfoBean> jiaoXloubeans = son.fromJson(response, new TypeToken<List<JsInfoBean>>() {
                        }.getType());
                        JslRecyclerView = findViewById(R.id.js_Recycle);
                        Log.i("教学楼信息", jiaoXloubeans.toString());

                        Toast.makeText(XuexiaojiaoshiActivity.this, "操作成功" + jiaoXloubeans, Toast.LENGTH_SHORT).show();
                        jsRecyclerAdapter = new JsRecyclerAdapter(XuexiaojiaoshiActivity.this, jiaoXloubeans);
                        JslRecyclerView.setLayoutManager(new LinearLayoutManager(XuexiaojiaoshiActivity.this, LinearLayoutManager.VERTICAL, false));
                        JslRecyclerView.setAdapter(jsRecyclerAdapter);
                    }
                })).start();


    }
}